$(document).ready(function() {
	time()
	GetAge()
    $('.SixwalkTimeBox-monthbox-month').click(function() {
    	var openclose = $(this).parent().find('.SixwalkTimeBox-monthbox-contentbox');
    	if(openclose.hasClass('month-open-close')){
    		openclose.removeClass('month-open-close');
    		$(this).css({'background':'#2d8cda','border':'1px solid #2d8cda'});
    	}else{
    		openclose.addClass('month-open-close');
    		$(this).css({'background':'#676b98','border':'1px solid #676b98'});
    	}
    })
    $("#totalTime").val(formatSeconds($("#totalTime").val()))
    if($("#sex").val()=="M"){
    	$("#sex").val("男")
    }else{
    	$("#sex").val("女")
    }

});
function GetAge() {
	var identityCard =$("#rAge").val();
    var len = (identityCard + "").length;
    if (len == 0) {
        return 0;
    } else {
        if ((len != 15) && (len != 18))//身份证号码只能为15位或18位其它不合法
        {
            return 0;
        }
    }
    var strBirthday = "";
    if (len == 18)//处理18位的身份证号码从号码中得到生日和性别代码
    {
        strBirthday = identityCard.substr(6, 4) + "/" + identityCard.substr(10, 2) + "/" + identityCard.substr(12, 2);
    }
    if (len == 15) {
        strBirthday = "19" + identityCard.substr(6, 2) + "/" + identityCard.substr(8, 2) + "/" + identityCard.substr(10, 2);
    }
    //时间字符串里，必须是“/”
    var birthDate = new Date(strBirthday);
    var nowDateTime = new Date();
    var age = nowDateTime.getFullYear() - birthDate.getFullYear();
    //再考虑月、天的因素;.getMonth()获取的是从0开始的，这里进行比较，不需要加1
    if (nowDateTime.getMonth() < birthDate.getMonth() || (nowDateTime.getMonth() == birthDate.getMonth() && nowDateTime.getDate() < birthDate.getDate())) {
        age--;
    }
    $("#rAge").val(age);
}
function time(){
	var timeStamp=[];
	  $("input[name='time']").each(function(index,a){
		  timeStamp.push($(this).val())
		  j=index
		});
	  if(timeStamp.length !=0){
			var date = new Date(timeStamp[0]);
			var html ='<div class="SixwalkTimeBox-year-title" >'+date.getFullYear()+'</div>'
				+'<div class="SixwalkTimeBox-year-content" >'
				+'<div class="SixwalkTimeBox-monthbox" id="month0">'
				+'<div class="SixwalkTimeBox-monthbox-month" >'+(date.getMonth()+1)+'月</div>'
				+'<div class="SixwalkTimeBox-monthbox-content" >'
				+'<div class="SixwalkTimeBox-monthbox-contentbox month-open-close" >'
				+'<div class="SixwalkTimeBox-daybox" id="day0">'
				+'<div class="SixwalkTimeBox-daybox-day" >'+(date.getMonth()+1)+'-'+date.getDate()+'</div>'
				+'<div class="SixwalkTimeBox-daybox-content" >'
				+'<div class="SixwalkTimeBox-daybox-day-content" id="hours0" onclick="loadDate(0)">'+date.getHours() + ':' + date.getMinutes()+'</div>'
				+'</div>'
				+'</div>'
				+'</div>'
				+'</div>'
				+'</div>'
				+'</div>';
		  $('#timeBox').html(html);
		  var strHtml;
		  var date1;
		  var date2;
		  var j;
			for(var i=0;i<j;i++){
				 date1 = new Date(timeStamp[i])
				 date2 =new Date(timeStamp[i+1])
				if(date1.getFullYear()==date2.getFullYear()){
					if(date1.getMonth()==date2.getMonth()){
						if(date1.getDate()==date2.getDate()){
							 strHtml='<div class="SixwalkTimeBox-daybox-day-content" onclick="loadDate('+(i+1)+')" id="hours'+(i+1)+'">'+date2.getHours() + ':' + date2.getMinutes()+'</div>'		
							$('#hours'+i).after(strHtml)
						}else{
							strHtml='<div class="SixwalkTimeBox-daybox" >'
								+'<div class="SixwalkTimeBox-daybox-day">'+(date2.getMonth()+1)+'-'+date2.getDate()+'</div>'
								+'<div class="SixwalkTimeBox-daybox-content" >'
									+'<div class="SixwalkTimeBox-daybox-day-content" onclick="loadDate('+(i+1)+')" id="hours'+(i+1)+'">'+date2.getHours() + ':' + date2.getMinutes()+'</div>'
								+'</div>'
							+'</div>'
							$('#hours'+i).parent().parent().parent().append(strHtml)
										}
					}else{
						strHtml ='<div class="SixwalkTimeBox-monthbox" >'
							+'<div class="SixwalkTimeBox-monthbox-month" >'+(date2.getMonth()+1)+'月</div>'
							+'<div class="SixwalkTimeBox-monthbox-content" >'
							+'<div class="SixwalkTimeBox-monthbox-contentbox month-open-close" >'
							+'<div class="SixwalkTimeBox-daybox" >'
							+'<div class="SixwalkTimeBox-daybox-day" >'+(date2.getMonth()+1)+'-'+date2.getDate()+'</div>'
							+'<div class="SixwalkTimeBox-daybox-content" >'
							+'<div class="SixwalkTimeBox-daybox-day-content"  onclick="loadDate('+(i+1)+')" id="hours'+(i+1)+'">'+date2.getHours() + ':' + date2.getMinutes()+'</div>'
							+'</div>'
							+'</div>'
							+'</div>'
							+'</div>'
							+'</div>';
						$('#hours'+i).parent().parent().parent().parent().parent().parent().append(strHtml)
					}
				}else{
					strHtml='<div class="SixwalkTimeBox-year"  >'
						+'<div class="SixwalkTimeBox-year-title" >'+date2.getFullYear()+'</div>'
					+'<div class="SixwalkTimeBox-year-content" >'
					+'<div class="SixwalkTimeBox-monthbox" >'
					+'<div class="SixwalkTimeBox-monthbox-month" >'+(date2.getMonth()+1)+'月</div>'
					+'<div class="SixwalkTimeBox-monthbox-content" >'
					+'<div class="SixwalkTimeBox-monthbox-contentbox month-open-close" >'
					+'<div class="SixwalkTimeBox-daybox" >'
					+'<div class="SixwalkTimeBox-daybox-day" >'+(date2.getMonth()+1)+'-'+date2.getDate()+'</div>'
					+'<div class="SixwalkTimeBox-daybox-content" >'
					+'<div class="SixwalkTimeBox-daybox-day-content" onclick="loadDate('+(i+1)+')" id="hours'+(i+1)+'">'+date2.getHours() + ':' + date2.getMinutes()+'</div>'
					+'</div>'
					+'</div>'
					+'</div>'
					+'</div>'
					+'</div>'
					+'</div>'
					+'</div>';
					$('#hours'+i).parent().parent().parent().parent().parent().parent().parent().parent().append(strHtml)
				}
		
			}
	  }

}
function loadDate(index){	
	var ids=[]
	$("input[name='id']").each(function(){
		ids.push($(this).val());
	});
	$('.SixwalkTimeBox-daybox-day-content').removeClass('dayclick');
	$('#hours' + index).addClass('dayclick');
	var id=ids[index];
	var url= '/data/getEcgReportById';
	$.ajax({
		url : url,
		type : 'POST',
		data : {
			"id" : id
		},
		success : function(data) {
			var ecg=data.ecgReportDO
			var heartRateDO = data.heartRateDO
			var abnormal=data.abnormalCountDO
			$('#avg').val(heartRateDO.aveHeartRate)
			$('#total').val(heartRateDO.totalNumber)
			$('#dayRate').val(heartRateDO.dayRate)
			$('#nightRate').val(heartRateDO.nightRate)
			$('#longDate').val(heartRateDO.longDate)
			$('#maxRate').val(heartRateDO.maxRate)
			/*$('#maxTime').val(formatDatetime(heartRateDO.maxTime))*/
			$('#slowRate').val(heartRateDO.slowRate)
		/*	$('#slowTime').val(formatDatetime(heartRateDO.slowTime))*/
			$('#bradycardia').val(heartRateDO.bradycardia)
			$('#tachycardia').val(heartRateDO.tachycardia)
			$('#fibrillationVf').val(abnormal.fibrillationVf)
			$('#ventricularTachycardia').val(abnormal.ventricularTachycardia)
			$('#multipleBeats').val(abnormal.multipleBeats)
			$('#twoWayBeat').val(abnormal.twoWayBeat)
			$('#occasionalBeats').val(abnormal.occasionalBeats)
			$('#ront').val(abnormal.ront)
			$('#polymorphicPvc').val(abnormal.polymorphicPvc)
			$('#duplexLaw').val(abnormal.duplexLaw)
			$('#sanlianLaw').val(abnormal.sanlianLaw)
			$('#number').val(ecg.number)
			$('#checkDate').val(formatDate(ecg.checkDate))	
			$('#reportTime').val(formatDatetime(ecg.reportTime))
			var str=formatDatetime(ecg.startTime)+"-"+formatDatetime(ecg.endTime)
			$('#startTime').val(str)
		    $('#totalTime').val(formatSeconds(ecg.totalTime))
		},
		error : function(jqXHR, textStatus, errorThrown) {
			console.log(errorThrown);
		},
		dataType : "json",
		async : true
	});
}
function formatSeconds(value) {
    var secondTime = parseInt(value);// 秒
    var minuteTime = 0;// 分
    var hourTime = 0;// 小时
    if(secondTime > 60) {//如果秒数大于60，将秒数转换成整数
        //获取分钟，除以60取整数，得到整数分钟
        minuteTime = parseInt(secondTime / 60);
        //获取秒数，秒数取佘，得到整数秒数
        secondTime = parseInt(secondTime % 60);
        //如果分钟大于60，将分钟转换成小时
        if(minuteTime > 60) {
            //获取小时，获取分钟除以60，得到整数小时
            hourTime = parseInt(minuteTime / 60);
            //获取小时后取佘的分，获取分钟除以60取佘的分
            minuteTime = parseInt(minuteTime % 60);
        }
    }
    var result = "" + parseInt(secondTime) + "秒";

    if(minuteTime > 0) {
        result = "" + parseInt(minuteTime) + "分" + result;
    }
    if(hourTime > 0) {
        result = "" + parseInt(hourTime) + "小时" + result;
    }
    return result;
}
	